<HTML><HEAD> <!-- ------ QUEUES ------ --> <SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers /* THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com and J. Brook Monroe, mrprogguy@techie.com Copyright (c)1998 by Charles River Media. All Rights Reserved. This applet can only be re-used or modifed by license holders of the JavaScript Cookbook CD-ROM. Credit must be given in the source code and this copyright notice must be maintained. If you do not hold a license to the JavaScript Cookbook, you may NOT duplicate or modify this code for your own use. Use at your own risk. No warranty is given or implied of the suitability of this applet for any specific application. Neither Erica Sadun, J. Brook Monroe nor Charles River Media will be held responsible for any unwanted effects due to the use of this applet or any derivative. */ // Create an Array function createArray(n) { t = new Array(50); for (var i = 0; i < n; i++) t[i] = 0; return t; } // Initialize Global Variables var queue = createArray(50); // 50 should be big enough var qStart = 0; // First item added to the queue var qEnd = 0; // Last item added to the queue // Show the queue function showqueue() { var answer = "" if(qStart != qEnd) { var i = qEnd; var fKeepOn = true; answer = queue[i]; while(fKeepOn) { i++; if(i == 50) i = 0; if(i == qStart) fKeepOn = false; else answer = answer + ':' + queue[i]; } } document.forms[0].queue.value = answer; document.forms[0].start.value = qStart; document.forms[0].end.value = qEnd; document.forms[0].input.focus(); document.forms[0].input.select(); } // Remove value from queue function remove() { if(qStart == qEnd) { alert('The queue is empty'); return; } document.forms[0].input.value = queue[qEnd]; qEnd++; if(qEnd > 49) qEnd = 0; showqueue(); } // Add value to queue function add() { queue[qStart++] = document.forms[0].input.value; if(qStart > 49) qStart = 0; showqueue(); } <!-- done hiding --></SCRIPT></HEAD> <BODY bgcolor="ffffff" link="0000ff" vlink="770077" onLoad="document.forms[0].input.focus();document.forms[0].input.select()"> <FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50 ALIGN = CENTER>Queues</H1></FONT> <FONT COLOR="770000"> Queues (like the stacks in another recipe) are easy to implement in JavaScript. You use an array, and two variables representing the start and end of the queue. </FONT></BLOCKQUOTE> <BR><BR> </SCRIPT> <CENTER><FORM><TABLE BORDER=1> <TR> <TD align=center colspan=2><input type="text" name="queue" value="" size=40>: queue</TD> </TR> <TR> <TD align=center colspan=2><input type="text" name="input" value="" size=8></TD> </TR> <TR> <TR> <TD>1st Entry <input type="text" size=3 name="end" value="0"></TD> <TD>Last Entry <input type="text" size=3 name="start" value="0"></TD> </TR> <TD align=center> <input type="button" value="Add" onClick="add()"></TD> <TD align=center> <input type="button" value="Remove" onClick="remove()"></TD> </TR> </TABLE></FORM></CENTER> <BR><BR> <FONT COLOR="007777"><H2>Discussion</H2></FONT> <FONT SIZE=4> Queues provide a good example of an array application; like stacks, they're easy to implement using JavaScript arrays. The kind of queue implemented here is called a <i>circular queue.</i> We've left something for you to do as an exercise: the code as written doesn't protect the end of the queue from overwriting the start of the queue if 51 items are added. How would you rewrite <FONT COLOR="007777">add()</FONT> to keep this from happening? </FONT> <FONT COLOR="770000"><PRE> var qStart = 0; // First item added to the queue var qEnd = 0; // Last item added to the queue // Show the queue function showqueue() { var answer = "" if(qStart != qEnd) { var i = qEnd; var fKeepOn = true; answer = queue[i]; while(fKeepOn) { i++; if(i == 50) i = 0; if(i == qStart) fKeepOn = false; else answer = answer + ':' + queue[i]; } } document.forms[0].queue.value = answer; document.forms[0].start.value = qStart; document.forms[0].end.value = qEnd; document.forms[0].input.focus(); document.forms[0].input.select(); } // Remove value from queue function remove() { if(qStart == qEnd) { alert('The queue is empty'); return; } document.forms[0].input.value = queue[qEnd]; qEnd++; if(qEnd > 49) qEnd = 0; showqueue(); } // Add value to queue; this will overwrite first value // if too many added function add() { queue[qStart++] = document.forms[0].input.value; if(qStart > 49) qStart = 0; showqueue(); } </PRE></FONT> <h5>Copyright ©1998 by Charles River Media, All Rights Reserved</h5> </BODY> </HTML>